/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-08-28
*/
package com.rzico.market.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.market.entity.Invoice;
import com.rzico.market.model.InvoiceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.market.mapper.InvoiceMapper;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 开票商品信息业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class InvoiceService extends BaseServiceImpl<Invoice, String> {

    @Autowired
    private InvoiceMapper invoiceMapper;

    @Override
    public BaseMapper<Invoice, String> getMapper() {
        return invoiceMapper;
    }

    /**
     * 保存开票商品信息
     * @param map
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Invoice saveInvoice(Map<String, Object> map) {
        Invoice invoice = new Invoice();
        invoice.setCreateDate(new Date());
        invoice.setDeleted(false);
        invoice.setState(false);
        invoice.setShippingDate((Date) map.get("shippingDate"));
        invoice.setSendDate((Date) map.get("sendDate"));
        invoice.setCompleteDate((Date) map.get("completeDate"));
        invoice.setOrderId((Long) map.get("orderId"));
        invoice.setOrderSn(map.get("orderSn").toString());
        invoice.setShippingId((Long) map.get("shippingId"));
        invoice.setShippingItemId((Long) map.get("shippingItemId"));
        invoice.setShippingSn(map.get("shippingSn").toString());
        invoice.setMemberId((Long)map.get("memberId"));
        invoice.setSupplierId((Long) map.get("supplierId"));
        invoice.setIsReturn((Boolean) map.get("isReturn"));
        invoice.setProductId((Long)map.get("productId"));
        invoice.setProductSn(map.get("productSn").toString());
        invoice.setProductName(map.get("productName").toString());
        invoice.setProductUnit(map.get("productUnit").toString());
        invoice.setProductCost(new BigDecimal(map.get("productCost").toString()));
        invoice.setProductPrice(new BigDecimal(map.get("productPrice").toString()));
        invoice.setSubPrice(new BigDecimal(map.get("subPrice").toString()));
        invoice.setProductQuantity(new BigDecimal(map.get("productQuantity").toString()));
        invoice.setInInvoiceQuantity(new BigDecimal(0));
        invoice.setEnterpriseId((Long) map.get("enterpriseId"));
        invoice.setOrderDiscount(new BigDecimal(map.get("orderDiscount").toString()));
        invoice.setProductDiscount(new BigDecimal(map.get("productDiscount").toString()));
        invoiceMapper.insertUseGeneratedKeys(invoice);
        Invoice result = super.findById(invoice.getId());
        return result;
    }

    /**
     * 开票商品及其支付信息
     * @param params
     * @return
     */
    public List<InvoiceVo> selectInvoiceInfoList(Map<String, Object> params){
        return invoiceMapper.selectInvoiceInfoList(params);
    }

}
